﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visiblox.Charts;
using Silverlight;
using SiteSL.ServiceReference1;
using System.Windows.Media.Imaging;

namespace SiteSL
{
    public partial class MainPage : UserControl
    {
        #region variable
        public static App ne;
        
        ServiceReference1.Service1Client svc = new ServiceReference1.Service1Client();
        patient p;
        List<ServiceReference1.EXE> allExeList = new List<ServiceReference1.EXE>();
        List<ServiceReference1.session> patientSessionList;
        List<ServiceReference1.problem> problemList;
        List<ServiceReference1.EXE> exeList;
        List<ServiceReference1.gamePlay> gamePlayList = new List<ServiceReference1.gamePlay>();
        List<ServiceReference1.session> allSessionOfPatientOfExeList = new List<ServiceReference1.session>();
        List<Image> jointList = new List<Image>();
        List<Color> colurList = new List<Color>();
        List<Button> jointBtnList = new List<Button>();
        int colorCount = 0;
        private ServiceReference1.EXE selectedExe;
        string stageName;
        int currentProblemID = 0;
        //string currentNavigationTag = "0";
        #endregion

        public MainPage()
        {
            InitializeComponent();
            App.retrieve = true;
            ne = new App();
            int x = Convert.ToInt32(ne.Param1);
            //this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            svc.retrievePatientProblemsCompleted += new EventHandler<ServiceReference1.retrievePatientProblemsCompletedEventArgs>(svc_retrievePatientProblemsCompleted);
            svc.retrievePatientProblemsAsync(1);
            svc.retrieveAllEXEofExeTypeCompleted += new EventHandler<ServiceReference1.retrieveAllEXEofExeTypeCompletedEventArgs>(svc_retrieveAllEXEofExeTypeCompleted);
            svc.retrieveAllEXEofExeTypeAsync(1);
            //svc.retrievePatientProblemsAsync(p.Uid);
            svc.retrieveAllEXECompleted += new EventHandler<retrieveAllEXECompletedEventArgs>(svc_retrieveAllEXECompleted);
            svc.retrieveAllEXEAsync();
            svc.retrieveSessionOfPatientCompleted += new EventHandler<retrieveSessionOfPatientCompletedEventArgs>(svc_retrieveSessionOfPatientCompleted);
            svc.retrieveSessionOfPatientAsync(1);
            //svc.retrieveSessionOfPatientAsync(p.Uid);
        }

        void svc_retrieveSessionOfPatientCompleted(object sender, retrieveSessionOfPatientCompletedEventArgs e)
        {
            patientSessionList = e.Result.ToList<ServiceReference1.session>();
        }

        void svc_retrieveAllEXECompleted(object sender, retrieveAllEXECompletedEventArgs e)
        {
            allExeList = e.Result.ToList<ServiceReference1.EXE>();
        }
        void svc_retrieveAllEXEofExeTypeCompleted(object sender, ServiceReference1.retrieveAllEXEofExeTypeCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                exeList = e.Result.ToList<ServiceReference1.EXE>();
            }
        }
        void svc_retrievePatientProblemsCompleted(object sender, ServiceReference1.retrievePatientProblemsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                problemList = e.Result.ToList<ServiceReference1.problem>();
                compulsoryWork();
                setPatientProblem();
            }
        }

        #region Treatment Analysis

        // 1st dropdown list
        private void cbxTreatment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            graph_rangeMotion.Series.Clear();
            cbxPerformance.SelectedIndex = -1;
            cbxYAxis.SelectedIndex = -1;
            sp_Attribute.Visibility = Visibility.Collapsed;
            sp_Stage.Visibility = Visibility.Collapsed;

            if (cbxTreatment.SelectedIndex != -1)
            {
                string treatment = cbxTreatment.SelectedItem.ToString();
                foreach (ServiceReference1.EXE ex in allExeList)
                {
                    if (treatment.Equals(ex.Name))
                    {
                        selectedExe = ex;
                    }
                }

                if (selectedExe.ExeStage.Count != 0) // got stage
                {
                    #region got exestage
                    //cbxPerformance.Visibility = Visibility.Collapsed;
                    //cbxPerformance.IsEnabled = false;
                    sp_Stage.Visibility = Visibility.Visible;
                    sp_Attribute.Visibility = Visibility.Collapsed;

                    List<string> stageNameListDisplay = new List<string>();

                    foreach (ServiceReference1.session patientSession in patientSessionList)
                    {
                        if (patientSession.ExeID == selectedExe.Uid)
                        {
                            List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                            patientGamePlayList = patientSession.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                            foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                            {
                                stageNameListDisplay.Add(gameplay.StageName);
                            }
                        }
                    }

                    stageNameListDisplay = stageNameListDisplay.Distinct().ToList();

                    cbxPerformance.ItemsSource = stageNameListDisplay;

                    #endregion
                }
                else
                {
                    #region no stage
                    stageName = "nostage";
                    sp_Attribute.Visibility = Visibility.Visible;
                    List<string> gameKeyList = new List<string>();
                    foreach (ServiceReference1.session patientSession in patientSessionList)
                    {
                        if (patientSession.ExeID == selectedExe.Uid)
                        {
                            List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                            patientGamePlayList = patientSession.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                            foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                            {
                                gameKeyList.Add(gameplay.GameKey);
                            }
                        }
                    }

                    gameKeyList = gameKeyList.Distinct().ToList();

                    cbxYAxis.SelectedIndex = -1;
                    cbxYAxis.Items.Clear();
                    foreach (string gameKey in gameKeyList)
                    {
                        CheckBox cbx = new CheckBox();
                        cbx.Checked += new RoutedEventHandler(cbx_Checked);
                        cbx.Unchecked += new RoutedEventHandler(cbx_Unchecked);
                        cbx.Content = gameKey;
                        cbxYAxis.Items.Add(cbx);
                    }

                    #endregion
                }
            }
        }

        // middle one AKA stage
        private void cbxPerformance_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cbxYAxis.SelectedIndex = -1;
            graph_rangeMotion.Series.Clear();
            //datagrid1.ItemsSource = null;
            sp_Attribute.Visibility = Visibility.Collapsed;

            if (cbxPerformance.SelectedIndex != -1)
            {
                stageName = cbxPerformance.SelectedItem.ToString();
                sp_Attribute.Visibility = Visibility.Visible;
                List<string> gameKeyList = new List<string>();
                foreach (ServiceReference1.session patientSession in patientSessionList)
                {
                    if (patientSession.ExeID == selectedExe.Uid)
                    {
                        List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                        patientGamePlayList = patientSession.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                        foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                        {
                            if (gameplay.StageName.Equals(stageName))
                            {
                                if (gameplay.GameKey.Contains("Angle"))
                                {
                                    gameKeyList.Add("Range");
                                }
                                else if (!gameplay.GameKey.Contains("Angle"))
                                {
                                    gameKeyList.Add(gameplay.GameKey);
                                }
                            }
                        }
                    }
                }

                gameKeyList = gameKeyList.Distinct().ToList();

                cbxYAxis.SelectedIndex = -1;
                cbxYAxis.Items.Clear();
                foreach (string gameKey in gameKeyList)
                {
                    CheckBox cbx = new CheckBox();
                    cbx.Content = gameKey;
                    cbx.Checked += new RoutedEventHandler(cbx_Checked);
                    cbx.Unchecked += new RoutedEventHandler(cbx_Unchecked);
                    cbxYAxis.Items.Add(cbx);
                }
            }
        }

        #region checkbox events in 3rd dropdownlist
        void cbx_Unchecked(object sender, RoutedEventArgs e)
        {
            createChart();
            createTable();
        }

        void cbx_Checked(object sender, RoutedEventArgs e)
        {
            createChart();
            createTable();
        }
        #endregion
        bool gotRange = false;
        private void createChart()
        {
            gotRange = false;
            //panel_moreInformation.Children.Clear();
            colorCount = 0;
            graph_rangeMotion.Series.Clear();
            Visiblox.Charts.DateTimeAxis asd = new DateTimeAxis();
            graph_rangeMotion.XAxis = asd;
            bool title = false;
            List<string> selectedKeyList = new List<string>();

            #region check for selected items

            foreach (CheckBox cbx in cbxYAxis.Items)
            {
                if (cbx.IsChecked == true)
                {
                    if (cbx.Content.ToString() != "Range")
                    {
                        selectedKeyList.Add(cbx.Content.ToString());
                    }
                    else
                    {
                        gotRange = true;
                    }
                }
            }
            #endregion

            #region noRange
            int keyCount = 0;
            for (int i1 = 0; i1 < selectedKeyList.Count; i1++)
            {
                string gameKey = selectedKeyList[i1];
                int count = 0;

                DataSeries<DateTime, double> temp = new DataSeries<DateTime, double>(gameKey);
                int xCount = 0;
                foreach (ServiceReference1.session ps in patientSessionList)
                {
                    if (ps.ExeID == selectedExe.Uid)
                    {
                        List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                        patientGamePlayList = ps.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                        List<int> gameplayIDlist = new List<int>();

                        foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                        {
                            if (!gameplayIDlist.Contains(gameplay.Uid))
                            {
                                //if (gameKey.Equals(gameplay.GameKey) && (gameplay.StageName.Equals(stageName) || stageName == "nostage"))
                                if (gameKey.Equals(gameplay.GameKey) && (gameplay.StageName.Equals(stageName) || gameplay.StageName.Equals("nostage")))
                                {
                                    xCount++;
                                    gameplayIDlist.Add(gameplay.Uid);
                                }
                            }
                        }
                    }
                }

                DateTime[] x = new DateTime[xCount];
                double[] y = new double[xCount];

                foreach (ServiceReference1.session patientSession in patientSessionList)
                {
                    string notes = "";
                    if (patientSession.ExeID == selectedExe.Uid)
                    {
                        List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                        patientGamePlayList = patientSession.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                        List<int> gameplayIDlist = new List<int>();

                        foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                        {
                            if (!gameplayIDlist.Contains(gameplay.Uid))
                            {
                                if (gameKey.Equals(gameplay.GameKey) && (gameplay.StageName.Equals(stageName) || stageName == "nostage"))
                                {
                                    gameplayIDlist.Add(gameplay.Uid);
                                    x[count] = patientSession.Dateofplay;
                                    y[count] = Convert.ToDouble(gameplay.GameValue);
                                    count++;
                                    //if (gameplay.SpecialInstructions != "")
                                    //{
                                    //    try
                                    //    {
                                    //        //notes += svc.retrieveJointName(gameplay.Joint) + " : " + gameplay.SpecialInstructions + ";";
                                    //    }
                                    //    catch (Exception)
                                    //    {
                                    //        notes += gameplay.Joint + " : " + gameplay.SpecialInstructions + ";";
                                    //    }
                                    //}

                                    if (title == false)
                                    {
                                        TextBlock textblock = new TextBlock();
                                        SolidColorBrush color = new SolidColorBrush();
                                        color.Color = Colors.White;
                                        textblock.Foreground = color;
                                        textblock.Text = "Notes";
                                        textblock.FontWeight = FontWeights.Bold;
                                        textblock.Margin = new Thickness(0, 5, 0, 5);
                                        //panel_moreInformation.Children.Add(textblock);
                                        title = true;
                                    }

                                    #region more information logic
                                    if (keyCount < patientSessionList.Count && count <= patientSessionList.Count)
                                    {
                                        TextBlock textblock = new TextBlock();
                                        SolidColorBrush color = new SolidColorBrush();
                                        color.Color = Colors.White;
                                        textblock.Foreground = color;
                                        textblock.Text = "Session : " + patientSession.Dateofplay;
                                        textblock.Margin = new Thickness(0, 5, 0, 5);
                                        //panel_moreInformation.Children.Add(textblock);
                                        TextBox textbox = new TextBox();
                                        string[] parts = notes.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                                        for (int i11 = 0; i11 < parts.Length; i11++)
                                        {
                                            if (i11 != parts.Length - 1)
                                            {
                                                textbox.Text += parts[i11] + Environment.NewLine;
                                            }
                                            else
                                                textbox.Text += parts[i11];
                                        }
                                        //panel_moreInformation.Children.Add(textbox);

                                    }
                                    #endregion
                                }
                            }
                        }
                    }
                    keyCount++;
                }
                for (int i = 0; i < x.Length; i++)
                {
                    temp.Add(new DataPoint<DateTime, double>() { X = x[i], Y = y[i] });
                }
                LineSeries ls = new LineSeries();

                SolidColorBrush color1 = new SolidColorBrush();
                color1.Color = colurList[colorCount];
                ls.LineStroke = color1;
                colorCount++;
                ls.LineStrokeThickness = 4;

                graph_rangeMotion.Series.Add(ls);

                graph_rangeMotion.Series[i1].DataSeries = temp;
            }

            #endregion

            #region gotRange
            bool showLegendRange = false;
            if (gotRange == true)
            {

                foreach (ServiceReference1.session patientSession in patientSessionList)
                {
                    DataSeries<DateTime, double> temp = new DataSeries<DateTime, double>("Range");
                    DateTime[] x = new DateTime[2];
                    double[] y = new double[2];
                    double gameValue1 = 0.0;
                    double gameValue2 = 0.0;
                    if (patientSession.ExeID == selectedExe.Uid)
                    {
                        List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                        patientGamePlayList = patientSession.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                        foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                        {
                            if (gameplay.GameKey.Contains("Angle") && (gameplay.StageName.Equals(stageName) || stageName == "nostage"))
                            {
                                if (gameValue1 == 0.0)
                                {
                                    try
                                    {
                                        gameValue1 = Convert.ToDouble(gameplay.GameValue);
                                        x[0] = patientSession.Dateofplay;
                                        y[0] = gameValue1;
                                        temp.Add(new DataPoint<DateTime, double>() { X = x[0], Y = y[0] });
                                    }
                                    catch (Exception) { }
                                }
                                else
                                {
                                    try
                                    {
                                        gameValue2 = Convert.ToDouble(gameplay.GameValue);
                                        x[1] = patientSession.Dateofplay;
                                        y[1] = gameValue2;
                                        temp.Add(new DataPoint<DateTime, double>() { X = x[1], Y = y[1] });
                                    }
                                    catch (Exception) { }
                                }
                            }
                        }

                        LineSeries ls = new LineSeries();

                        SolidColorBrush color1 = new SolidColorBrush();
                        color1.Color = Colors.Red;
                        ls.LineStroke = color1;
                        colorCount++;
                        ls.LineStrokeThickness = 4;
                        ls.DataSeries = temp;
                        if (showLegendRange == false)
                        {
                            showLegendRange = true;
                        }
                        else
                            ls.IsDisplayedOnLegend = false;
                        graph_rangeMotion.Series.Add(ls);

                    }
                }
            }

            #endregion

            #region unwanted
            //ArrayList gameKey = new ArrayList();
            //string performance = "";
            //cbxYAxis.Visibility = Visibility.Visible;
            //if (cbxPerformance.Visibility == Visibility.Collapsed)
            //{
            //}
            //else
            //{
            //    performance = cbxPerformance.SelectedItem.ToString();
            //}


            ////foreach (ServiceReference1.stage s in stageList)
            ////{
            ////    if (performance.Equals(s.Name))
            ////    {
            ////        id = s.Uid;
            ////        svc.retrieveSessionOfPatientofExeCompleted += new EventHandler<ServiceReference1.retrieveSessionOfPatientofExeCompletedEventArgs>(svc_retrieveSessionOfPatientofExeCompleted);
            ////        svc.retrieveSessionOfPatientofExeAsync(p.Uid, id);
            ////    }
            ////}

            //foreach (ServiceReference1.EXE ex in allExeList)
            //{
            //    if (ex.Uid == id)
            //    {
            //        foreach (ServiceReference1.gamePlay g in gamePlayList)
            //        {
            //            if (g.StageID == id)
            //            {
            //                gameKey.Add(g.GameKey);
            //            }
            //        }

            //        for (int i = 0; i < cbxYAxis.Items.Count; i++)
            //        {
            //            cbxYAxis.Items.Remove(i);
            //        }

            //        for (int i = 0; i < gameKey.Count; i++)
            //        {
            //            cbxYAxis.Items.Add(gameKey[i]);
            //        }
            //    }
            //}

            ////svc.retrieveSessionOfPatientofExeCompleted += new EventHandler<ServiceReference1.retrieveSessionOfPatientofExeCompletedEventArgs>(svc_retrieveSessionOfPatientofExeCompleted);
            ////svc.retrieveSessionOfPatientofExeAsync(p.Uid, id);

            //gamePlayList.Clear();

            //ArrayList allSessionPatientID = new ArrayList();
            //ArrayList allSessionGameKey = new ArrayList();
            //ArrayList allSessionGameValue = new ArrayList();

            //foreach (ServiceReference1.session s in allSessionOfPatientOfExeList)
            //{
            //    allSessionPatientID.Add(s.SessionID);
            //}

            //foreach (ServiceReference1.gamePlay s in allSessionPatientID)
            //{
            //    allSessionGameKey.Add(s.GameKey);
            //    allSessionGameValue.Add(s.GameValue);
            //}

            ////for (int i = 0; i < gamePlayList.Count(); i++)
            ////{
            ////    if (gamePlayList[i].ExeID != 1)
            ////    {
            ////        gamePlayList.Add(gamePlayList[i]);
            ////    }
            ////}
            ////gamePlayList.Reverse();

            //for (int i = 0; i < allSessionPatientID.Count; i++)
            //{
            //    for (int x = 0; x < allSessionGameKey.Count; i++)
            //    {
            //        DataSeries<double, double> topLeft = new DataSeries<double, double>(allSessionGameKey[x].ToString());
            //        topLeft.Title = allSessionGameKey[x].ToString();

            //        LineSeries ls = new LineSeries();
            //        graph_rangeMotion.Series.Add(ls);
            //        //ls.NormalStyle = 
            //        Style style1 = new System.Windows.Style();
            //        SolidColorBrush red = new SolidColorBrush();
            //        red.Color = Colors.Red;
            //        ls.LineStroke = red;

            //        graph_rangeMotion.Series[0].DataSeries = topLeft;
            //    }
            //}
            //DataSeries<double, double> topLeft = new DataSeries<double, double>("Top Left");
            //DataSeries<double, double> topRight = new DataSeries<double, double>("Top Right");
            //DataSeries<double, double> bottomLeft = new DataSeries<double, double>("Bottom Left");
            //DataSeries<double, double> bottomRight = new DataSeries<double, double>("Bottom Right");
            //topLeft.Title = "Top Left";
            //topRight.Title = "Top Right";
            //bottomRight.Title = "Bottom Right";
            //bottomLeft.Title = "Bottom Left";

            //Add the data points to the data series according to the correct equation
            //for (int i = 0; i < gamePlayList.Count(); i++)
            //{
            //    if (gamePlayList[i] != null && !gamePlayList[i].AvgTime1.Equals(""))
            //    {
            //        topLeft.Add(new DataPoint<double, double>() { X = i, Y = Convert.ToDouble(gamePlayList[i].AvgTime1.ToString()) });
            //        topRight.Add(new DataPoint<double, double>() { X = i, Y = Convert.ToDouble(gamePlayList[i].AvgTime2.ToString()) });
            //        bottomRight.Add(new DataPoint<double, double>() { X = i, Y = Convert.ToDouble(gamePlayList[i].AvgTime3.ToString()) });
            //        bottomLeft.Add(new DataPoint<double, double>() { X = i, Y = Convert.ToDouble(gamePlayList[i].AvgTime4.ToString()) });
            //        topLeft[i].Tag = Convert.ToDouble(gamePlayList[i].AvgTime1.ToString());
            //        topRight[i].Tag = Convert.ToDouble(gamePlayList[i].AvgTime2.ToString());
            //        bottomRight[i].Tag = Convert.ToDouble(gamePlayList[i].AvgTime3.ToString());
            //        bottomLeft[i].Tag = Convert.ToDouble(gamePlayList[i].AvgTime4.ToString());

            //    }
            //}

            //LineSeries ls = new LineSeries();
            //graph_rangeMotion.Series.Add(ls);
            ////ls.NormalStyle = 
            //Style style1 = new System.Windows.Style();
            //SolidColorBrush red = new SolidColorBrush();
            //red.Color = Colors.Red;
            //ls.LineStroke = red;

            //graph_rangeMotion.Series[0].DataSeries = topLeft;
            //graph_rangeMotion.Series[1].DataSeries = topRight;
            //graph_rangeMotion.Series[2].DataSeries = bottomRight;
            //graph_rangeMotion.Series[3].DataSeries = bottomLeft;
            #endregion
        }

        private void createTable()
        {
            List<string> selectedKeyList = new List<string>();

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);

            DataColumn cl1 = new DataColumn("Session", typeof(string));
            //cl1.MaxLength = 100;
            dt.Columns.Add(cl1);

            foreach (CheckBox cbx in cbxYAxis.Items)
            {
                if (cbx.IsChecked == true)
                {
                    selectedKeyList.Add(cbx.Content.ToString());
                    DataColumn cl = new DataColumn(cbx.Content.ToString(), typeof(string));
                    //cl.MaxLength = 100;
                    dt.Columns.Add(cl);
                }
            }

            foreach (ServiceReference1.session ps in patientSessionList)
            {
                if (ps.ExeID == selectedExe.Uid)
                {
                    List<ServiceReference1.gamePlay> patientGamePlayList = new List<ServiceReference1.gamePlay>();
                    patientGamePlayList = ps.GamePlaylist.ToList<ServiceReference1.gamePlay>();

                    List<string> addedKey = new List<string>();

                    DataRow rw = dt.NewRow();
                    dt.Rows.Add(rw);
                    foreach (ServiceReference1.gamePlay gameplay in patientGamePlayList)
                    {
                        foreach (string key in selectedKeyList)
                        {
                            if (gameplay.GameKey.Equals(key) && (gameplay.StageName.Equals(stageName) || stageName == "nostage"))
                            {
                                if (!addedKey.Contains(gameplay.GameKey))
                                {
                                    addedKey.Add(gameplay.GameKey);
                                    //logic to add it in.

                                    rw[gameplay.GameKey] = gameplay.GameValue.ToString();
                                }
                            }
                        }
                        //if (!gameplayIDlist.Contains(gameplay.Uid))
                        //{
                        //    if (gameKey.Equals(gameplay.GameKey) && (gameplay.StageName.Equals(stageName) || stageName == "nostage"))
                        //    {
                        //        xCount++;
                        //        gameplayIDlist.Add(gameplay.Uid);
                        //    }

                        //}
                    }
                    rw["Session"] = ps.Dateofplay.ToString();
                }
            }
            //datagrid1.ItemsSource = ds.Tables[0].DefaultView;
        }

        #endregion

        //private void lblSever_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    // set the color for severity
        //    int severity = 0;
        //    try
        //    {
        //        severity = Convert.ToInt16(lblSever.Text);
        //        severity = 6 - severity;
        //    }
        //    catch (Exception)
        //    {
        //        MessageBox.Show("Please enter 1 - 5 for severity");
        //    }
        //    if (severity < 0 || severity > 5)
        //    {
        //        MessageBox.Show("Please enter 1 - 5 for severity");
        //    }
        //    else
        //    {
        //        pbrSever.Value = (severity * 20);
        //        if (pbrSever.Value >= 60)
        //        {
        //            pbrSever.Foreground = new SolidColorBrush(Colors.Red);// Brushes.Red;
        //        }
        //        else if (pbrSever.Value < 60 && pbrSever.Value >= 20)
        //        {
        //            pbrSever.Foreground = new SolidColorBrush(Colors.Yellow); //Brushes.Yellow;
        //        }
        //        else
        //        {
        //            pbrSever.Foreground = new SolidColorBrush(Colors.Green); //Brushes.DarkGreen;
        //        }
        //    }
        //}

        private void tbx_table_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            panel_graph.Visibility = Visibility.Collapsed;
            panel_table.Visibility = Visibility.Visible;
            //createTable();
        }

        private void tbx_graph_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            panel_graph.Visibility = Visibility.Visible;
            panel_table.Visibility = Visibility.Collapsed;
            //createChart();
        }

        //after patientlogin
        //private void doWorkAfterPatientSelected(patient patient1)
        //{
        //    p = patient1;

        //    setPatientProblem();
        //}
        void compulsoryWork()
        {
            # region add color into list
            //colurList.Add(Colors.Red);
            //colurList.Add(Colors.Gold);
            colurList.Add(Colors.Cyan);
            colurList.Add(Colors.Black);
            colurList.Add(Colors.Brown);
            colurList.Add(Colors.Green);
            colurList.Add(Colors.Black);
            //colurList.Add(Colors.SpringGreen);
            colurList.Add(Colors.Blue);
            colurList.Add(Colors.Gray);
            colurList.Add(Colors.Magenta);
            colurList.Add(Colors.Orange);
            colurList.Add(Colors.Purple);
            colurList.Add(Colors.Yellow);
            colurList.Add(Colors.LightGray);
            # endregion

            # region add image into a list
            jointList.Add(joint_1);
            jointList.Add(joint_2);
            jointList.Add(joint_3);
            jointList.Add(joint_4);
            jointList.Add(joint_5);
            jointList.Add(joint_6);
            jointList.Add(joint_7);
            jointList.Add(joint_8);
            jointList.Add(joint_9);
            jointList.Add(joint_10);
            jointList.Add(joint_11);
            jointList.Add(joint_12);
            jointList.Add(joint_13);
            jointList.Add(joint_14);
            #endregion

            # region add button into a list
            jointBtnList.Add(btn_joint_1);
            jointBtnList.Add(btn_joint_2);
            jointBtnList.Add(btn_joint_3);
            jointBtnList.Add(btn_joint_4);
            jointBtnList.Add(btn_joint_5);
            jointBtnList.Add(btn_joint_6);
            jointBtnList.Add(btn_joint_7);
            jointBtnList.Add(btn_joint_8);
            jointBtnList.Add(btn_joint_9);
            jointBtnList.Add(btn_joint_10);
            jointBtnList.Add(btn_joint_11);
            jointBtnList.Add(btn_joint_12);
            jointBtnList.Add(btn_joint_13);
            jointBtnList.Add(btn_joint_14);
            # endregion
        }

        private void setPatientProblem()
        {
            for (int i = 0; i < problemList.Count(); i++)
            {
                ServiceReference1.problem temp = problemList[i];
                int joint = Convert.ToInt16(temp.Joint);
                foreach (Image image in jointList)
                {
                    if (Convert.ToInt16(image.Tag) == joint)
                    {
                        // change the image
                        image.Source = new BitmapImage(new Uri("jointBad.png", UriKind.Relative));
                        image.MouseEnter += new MouseEventHandler(iBad_MouseEnter);
                        image.MouseLeave += new MouseEventHandler(iBad_MouseLeave);
                        image.Tag = temp.Uid;
                        continue;
                    }
                }
                foreach (Button btn in jointBtnList)
                {
                    if (Convert.ToInt16(btn.Tag) == joint)
                    {
                        btn.Click += new RoutedEventHandler(joint_btn_Click);
                        btn.Tag = temp.Uid;
                        //btn.ToolTip = temp.Description;
                        continue;
                    }
                }
            }

            //hide the blue color joints
            foreach (Button btn in jointBtnList)
            {
                if (Convert.ToInt16(btn.Tag) <= 14)
                {
                    btn.Visibility = Visibility.Collapsed;
                }
            }
        }

        # region MouseEnter MouseLeave events for joints image
        void i_MouseLeave(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointFine.png", UriKind.Relative));
        }

        void i_MouseEnter(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointFineH.png", UriKind.Relative));
        }

        void iBad_MouseLeave(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointBad.png", UriKind.Relative));
        }

        void iBad_MouseEnter(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointBadH.png", UriKind.Relative));
        }
        #endregion

        int selectedProblemUID;
        void joint_btn_Click(object sender, RoutedEventArgs e)
        {
            int problemUID = Convert.ToInt16(((Button)sender).Tag);
            selectedProblemUID = problemUID;
            if (currentProblemID != problemUID)
            {
                cbxTreatment.SelectedIndex = -1;
                cbxPerformance.SelectedIndex = -1;
                cbxYAxis.SelectedIndex = -1;

                cbxTreatment.ItemsSource = null;
                sp_Stage.Visibility = Visibility.Collapsed;
                sp_Attribute.Visibility = Visibility.Collapsed;
                stageName = "";
                selectedExe = null;
                currentProblemID = problemUID;
                setProblem(problemUID);
                setEXE(problemUID);

                //if (allExeList.Count == 0)
                //{
                //    try
                //    {
                //        allExeList = svc.retrieveAllEXE();
                //    }
                //    catch (Exception)
                //    { }
                //}
                List<ServiceReference1.EXE> treatmentList = new List<ServiceReference1.EXE>();
                foreach (ServiceReference1.EXE x in allExeList)
                {
                    foreach (string joint in x.Tag)
                    {
                        if (joint == tbx_joint_Copy.Text)
                        {
                            treatmentList.Add(x);
                        }
                    }
                }

                treatmentList = treatmentList.Distinct().ToList();
                cbxTreatment.Items.Clear();
                foreach (ServiceReference1.EXE x in treatmentList)
                {
                    cbxTreatment.Items.Add(x.Name);
                }

                //foreach (ServiceReference1.problem p in problemList)
                //{
                //    foreach (ServiceReference1.EXE ex in allExeList)
                //    {
                //        if (p.Uid == problemUID)
                //        {
                //            cbxTreatment.Items.Add(ex.Name);//get name for id
                //        }
                //    }
                //}
                //if (currentNavigationTag.Equals("0"))
                //{
                //    BrushConverter brush = new BrushConverter();
                //    currentNavigationTag = "2";
                //    //btn_treatmentManager.Background = (Brush)brush.ConvertFromString("#FF1123BC");
                //    //btn_treatmentManager.Height = 24;
                //    //panel_treatmentManager.Visibility = Visibility.Visible;
                //}
            }
        }

        // set EXE for recommended and assigned
        private void setEXE(int problemUID)
        {
            //List<ServiceReference1.EXE> exeRecommendedListJoint = new List<ServiceReference1.EXE>();
            //List<ServiceReference1.EXE> exeAssignedListJoint = new List<ServiceReference1.EXE>();
            ServiceReference1.problem problem = new ServiceReference1.problem();

            foreach (ServiceReference1.problem p in problemList)
            {
                if (p.Uid == currentProblemID)
                {
                    problem = p;
                }
            }

            // exeRecommendedListJoint.Clear();

            //foreach (ServiceReference1.EXE exeRecommended in exeList)
            //{
            //    foreach (String jointTemp in exeRecommended.Tag)
            //    {
            //        if (jointTemp == problem.Joint)
            //        {
            //            exeRecommendedListJoint.Add(exeRecommended);
            //        }
            //    }
            //}

            //exeAssignedListJoint.Clear();
            List<int> addedExeIDAssigned = new List<int>();
            //List<ServiceReference1.EXE> exeTempList = exeRecommendedListJoint.Distinct().ToList();
            //foreach (ServiceReference1.EXE exeAssigned in exeAssignedList)
            //{
            //    foreach (String jointTemp in exeAssigned.Tag)
            //    {
            //        if (jointTemp == problem.Joint)
            //        {
            //            if (!addedExeIDAssigned.Contains(exeAssigned.Uid))
            //            {
            //                exeAssignedListJoint.Add(exeAssigned);
            //            }

            //            addedExeIDAssigned.Add(exeAssigned.Uid);
            //            foreach (ServiceReference1.EXE exeTemp in exeTempList)
            //            {
            //                if (exeTemp.Uid == exeAssigned.Uid)
            //                {
            //                    exeRecommendedListJoint.Remove(exeTemp);
            //                    //toAdd.Add(exeAssigned.Name);
            //                }
            //            }
            //        }
            //    }
            //}

            //toAdd = toAdd.Distinct().ToList();
            //cbxTreatment.ItemsSource = toAdd;
            //List<ServiceReference1.EXE> tempexe = exeRecommendedListJoint.Except(exeAssignedListJoint).ToList();
            //exeRecommendedListJoint.Clear();
            //foreach (ServiceReference1.EXE exe in tempexe)
            //{

            //    exeRecommendedListJoint.Add(exe);
            //}

            //lbx_exe.ItemsSource = exeRecommendedListJoint;
            //lbx_exeAssigned.ItemsSource = exeAssignedListJoint;

            //lbx_exe.Items.Clear();
            //lbx_exeAssigned.Items.Clear();
            //List<ServiceReference1.EXE> exeRecommendedListJointTemp = exeRecommendedListJoint.Distinct().ToList();
            //List<ServiceReference1.EXE> exeAssignedListJointTemp = exeAssignedListJoint.Distinct().ToList();
            //foreach (ServiceReference1.EXE temp in exeRecommendedListJointTemp)
            //{
            //    exeItem exeitem = new exeItem(temp);
            //    lbx_exe.Items.Add(exeitem);
            //}

            //foreach (ServiceReference1.EXE temp in exeAssignedListJointTemp)
            //{
            //    exeItem exeitem = new exeItem(temp);
            //    lbx_exeAssigned.Items.Add(exeitem);
            //}
        }

        private void setProblem(int problemUID)
        {
            for (int i = 0; i < problemList.Count(); i++)
            {
                ServiceReference1.problem temp = problemList[i];
                if (temp.Uid == problemUID)
                {
                    tbx_joint_Copy.Text = temp.Joint;
                    tbx_joint.Text = temp.JointName;

                    //tbxReport.Text = "";
                    string[] parts = temp.Description.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    //for (int i1 = 0; i1 < parts.Length; i1++)
                    //{
                    //    tbxReport.Text += parts[i1] + Environment.NewLine;
                    //}

                    //tbxNote.Text = "";
                    //parts = temp.Notes.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    //for (int i1 = 0; i1 < parts.Length; i1++)
                    //{
                    //    tbxNote.Text += parts[i1] + Environment.NewLine;
                    //}

                    lblSever.Text = temp.Severity.ToString();
                    // set the color for severity
                    int severity = 6 - temp.Severity;
                    pbrSever.Value = (severity * 20);
                    if (pbrSever.Value >= 60)
                    {
                        pbrSever.Foreground = new SolidColorBrush(Colors.Red); //Brushes.Red;
                    }
                    else if (pbrSever.Value < 60 && pbrSever.Value >= 20)
                    {
                        pbrSever.Foreground = new SolidColorBrush(Colors.Yellow); //Brushes.Yellow;
                    }
                    else
                    {
                        pbrSever.Foreground = new SolidColorBrush(Colors.Green); //Brushes.DarkGreen;
                    }
                }
            }
        }
    }
}